/* eslint-disable */
///////////////////////////////////////////////

export default cc.Class({
    extends: cc.Component,

    properties: {
        delay: {
            default: 2.0,
        },
        duration: {
            default: 0.3,
        },
        wait: {
            default: 0.7,
        },
        endName: {
            default: 'action_end',
        },
        doing: {
            default: false,
            notify: function() {
                if (this.lastdoing !== this.doing) {
                    this.lastdoing = this.doing;
                    this.update_doing = true;
                }
            },
        },
    },

    update() {
        if (this.update_doing) {
            this.update_doing = false;
            if (this.doing) {
                this.doStart();
            } else {
                this.doStop();
            }
        }
    },

    doStart() {
        this.doStop();
        //console.log('flip start');
        this.node.scaleX = 1;
        if (this.wait > 0) {
            if (this.delay > 0) {
                this.delay_action = cc.spawn(
                    cc.sequence(
                        cc.scaleTo(this.duration, 0, 1),
                        cc.delayTime(this.wait),
                        cc.scaleTo(this.duration, 1, 1)
                    ),
                    cc.sequence(
                        cc.delayTime(this.delay),
                        cc.callFunc(this.doEnd, this)
                    )
                );
            } else {
                this.delay_action = cc.sequence(
                    cc.scaleTo(this.duration, 0, 1),
                    cc.delayTime(this.wait),
                    cc.scaleTo(this.duration, 1, 1),
                    cc.callFunc(this.doEnd, this)
                );
            }
        } else {
            if (this.delay > 0) {
                this.delay_action = cc.spawn(
                    cc.sequence(
                        cc.scaleTo(this.duration, 0, 1),
                        cc.scaleTo(this.duration, 1, 1)
                    ),
                    cc.sequence(
                        cc.delayTime(this.delay),
                        cc.callFunc(this.doEnd, this)
                    )
                );
            } else {
                this.delay_action = cc.sequence(
                    cc.scaleTo(this.duration, 0, 1),
                    cc.scaleTo(this.duration, 1, 1),
                    cc.callFunc(this.doEnd, this)
                );
            }
        }
        this.node.runAction(this.delay_action);
    },

    doStop() {
        //console.log('flip stop');
        if (this.delay_action) {
            this.node.scaleX = 1;
            this.node.stopAction(this.delay_action);
            this.delay_action = null;
        }
    },

    doEnd() {
        // console.log('flip end');
        this.lastdoing = false;
        this.doing = false;
        this.delay_action = null;
        this.node.emit(this.endName, this);
    },
});
